﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BBB.Valuta.Entity;

namespace BBB.Valuta
{
    public partial class AddReceiptForm : Form
    {
        public delegate void ArgReceipt(Receipt receipt);

        public event ArgReceipt AddReceipt;

        private Receipt receipt;

        public AddReceiptForm()
        {
            receipt = new Receipt();
            InitializeComponent();
            currensyComboBox.Items.AddRange(Settings.GetInstance().Valuta.Select(v => v.Name).ToArray());
            currensyComboBox.SelectedIndex = 0;
            dateDateTimePicker.Value = DateTime.Now;
            Text = "Нова квитанція";
            addButton.Text = "Добавити";
        }

        public AddReceiptForm(Receipt rec):this()
        {
            receipt = rec;
            dateDateTimePicker.Value = rec.OutDate;
            
            if (rec.IsSale)
                saleRadioButton.Checked = true;
            else 
                payRadioButton.Checked = true;
            kursNumericUpDown.Value = rec.Kurs;
            numberTextBox.Text = rec.Number;
            timeMaskedTextBox.Text = rec.OutTime.ToString("HH:mm:yy");
            sumNumericUpDown.Value = rec.SummaIn;
            currensyComboBox.SelectedIndex = currensyComboBox.Items.IndexOf(rec.Val);
            Text = "Редагування квитанції";
            addButton.Text = "Змінити";
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            try
            {
                receipt.IsSale = saleRadioButton.Checked;
                receipt.Kurs = kursNumericUpDown.Value;
                receipt.Number = numberTextBox.Text;
                receipt.OutTime = DateTime.Parse(timeMaskedTextBox.Text);
                receipt.OutDate = dateDateTimePicker.Value;
                receipt.SummaIn = sumNumericUpDown.Value;
                receipt.Val = currensyComboBox.Text;
                receipt.SummaOut =
                    Decimal.Parse(receipt.IsSale
                                      ? (receipt.SummaIn/receipt.Kurs).ToString("0.00")
                                      : (receipt.SummaIn*receipt.Kurs).ToString("0.00"));
                if (addButton.Text == "Добавити")
                    AddReceipt(receipt);
                DialogResult = DialogResult.OK;
            }
            catch (FormatException)
            {
                MessageBox.Show("Невірно введені дані", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            catch (ArgumentOutOfRangeException)
            {
                MessageBox.Show("Невірно введені дані", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (ArgumentNullException)
            {
                MessageBox.Show("Невірно введені дані", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception)
            {
                MessageBox.Show("Неможливо добавити запис", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void sumNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                string decSep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
                sumClLabel.Text = string.Format("Сума до видачі: {0}",
                                                !saleRadioButton.Checked
                                                    ? (kursNumericUpDown.Value*sumNumericUpDown.Value).ToString("0.00")
                                                    : (sumNumericUpDown.Value / kursNumericUpDown.Value).ToString("0.00"));
            }
            catch(Exception)
            {
                sumClLabel.Text = string.Format("Сума до видачі: 0,00");
            }
        }

        private void timeMaskedTextBox_Leave(object sender, EventArgs e)
        {
            DateTime time;
            if (DateTime.TryParseExact(timeMaskedTextBox.Text, "HH:mm:ss", null, DateTimeStyles.None, out time))
            {
                timeErrorProvider.SetError(timeMaskedTextBox, "Error");
            }

        }
    }
}
